<template>
	<view class="container-bottom-btns">
		<view class="form">
			<view class="form-column-item">
				<view class="label">出售类别</view>
				<view class="picker-content" @tap="$refs.productType.open()">
					<view :class="{'placeholder-picker': !form.productType, 'picker-text': form.productType}">
						{{form.productType ? productTypeStr : '请选择出售类别'}}
					</view>
					<u-icon name="arrow-down"></u-icon>
				</view>
			</view>
			<view class="form-column-item">
				<view class="label">出售类型</view>
				<view class="picker-content" @tap="$refs.sellType.open()">
					<view :class="{'placeholder-picker': !form.putType, 'picker-text': form.putType}">
						{{form.putType ? sellTypeStr : '请选择出售类型'}}
					</view>
					<u-icon name="arrow-down"></u-icon>
				</view>
			</view>
			<view class="form-column-item">
				<view class="label">分类</view>
				<uni-data-picker  ref="category" :localdata="categoryList" popup-title="请选择分类" @change="onchange"
					v-model="form.categoryId" @nodeclick="onnodeclick" ></uni-data-picker>
			</view>
			<view class="form-column-item" v-if="form.productType === productType.device.value">
				<view class="label">设备品牌</view>
				<view class="content">
					<input type="text" placeholder="请输入品牌" v-model="form.hgProductEquipmentSell.equipmentBrand"/>
				</view>
			</view>
			<view class="form-column-item" v-if="form.productType === productType.buildStuff.value">
				<view class="label">库存</view>
				<view class="content">
					<input type="number" v-model="form.hgProductMaterialSell.stock" placeholder="请输入库存" />
					<text class="store-unit">/个/节/米/吨</text>
				</view>
			</view>
			<view class="form-column-item" v-if="form.productType === productType.buildStuff.value">
				<view class="label">单价</view>
				<view class="content">
					<input type="digit" v-model="form.hgProductMaterialSell.price" placeholder="请输入单价" />
				</view>
			</view>
			<view class="form-column-item" v-if="form.productType === productType.device.value">
				<view class="label">设备型号</view>
				<view class="content">
					<input type="text" placeholder="请输入型号" v-model="form.hgProductEquipmentSell.equipmentNo" />
				</view>
			</view>
			<view class="form-column-item" v-if="form.productType === productType.device.value  && form.putType === sellType.factory.value">
				<view class="label">设备规格</view>
				<view class="content">
					<view class="add-sku flex-xy-center" @tap="$myUni.navTo('/pages/product/form-sku?from=add')">
						设置规格
					</view>
				</view>
			</view>
			<view class="form-column-item" v-if="form.productType === productType.device.value  && form.putType === sellType.used.value">
				<view class="label">出厂日期</view>
				<view class="picker-content" @tap="showFactoryDate = true">
					<view :class="{'placeholder-picker': !form.hgProductEquipmentSell.factoryDate, 'picker-text': form.hgProductEquipmentSell.factoryDate}">
						{{form.hgProductEquipmentSell.factoryDate ? form.hgProductEquipmentSell.factoryDate : '请选择出厂日期'}}
					</view>
					<u-icon name="arrow-down"></u-icon>
				</view>
			</view>
			<view class="form-column-item" v-if="form.productType === productType.device.value  && form.putType === sellType.used.value && !productId">
				<view class="label">购机价格</view>
				<view class="content">
					<input type="digit" placeholder="请输入购机价格" v-model="form.hgProductEquipmentSell.buyPrice"/>
				</view>
			</view>
			<view class="form-column-item" v-if="form.productType === productType.device.value  && form.putType === sellType.used.value">
				<view class="label">工作小时</view>
				<view class="content">
					<input type="digit" v-model="form.hgProductEquipmentSell.workHours" placeholder="请输入工作小时" />
				</view>
			</view>
			<view class="form-column-item" v-if="form.productType === productType.device.value  && form.putType === sellType.used.value && !productId">
				<view class="label">机况状态</view>
				<view class="picker-content" @tap="$refs.oldNewStatus.open()">
					<view :class="{'placeholder-picker': !form.hgProductEquipmentSell.status, 'picker-text': form.hgProductEquipmentSell.status}">
						{{form.hgProductEquipmentSell.status ? oldNewStatusStr : '请选择机况状态'}}
					</view>
					<u-icon name="arrow-down"></u-icon>
				</view>
			</view>
			<view class="form-column-item" v-if="form.productType === productType.device.value">
				<view class="label">整机序列号</view>
				<view class="content">
					<input type="text" placeholder="请输入整机序列号" v-model="form.hgProductEquipmentSell.serialNo"/>
				</view>
			</view>
			<view class="form-column-item" v-if="form.productType === productType.device.value">
				<view class="label">售价</view>
				<view class="content">
					<input type="digit" placeholder="请输入金额" v-model="form.hgProductEquipmentSell.price"/>
				</view>
			</view>
			<view class="form-column-item" v-if="form.putType === sellType.factory.value">
				<view class="label">付款方式</view>
				<view class="picker-content" @tap="$refs.payType.open()">
					<view :class="{'placeholder-picker': !form.hgProductEquipmentSell.paymentWay, 'picker-text': form.hgProductEquipmentSell.paymentWay}">
						{{form.hgProductEquipmentSell.paymentWay ? payTypeStr : '请选择付款方式'}}
					</view>
					<u-icon name="arrow-down"></u-icon>
				</view>
			</view>
			<view class="form-column-item" v-if="form.productType === productType.device.value">
				<view class="label">设备状态</view>
				<view class="picker-content" @tap="$refs.deviceStatus.open()">
					<view :class="{'placeholder-picker': !form.hgProductEquipmentSell.equipmentStatus, 'picker-text': form.hgProductEquipmentSell.equipmentStatus}">
						{{form.hgProductEquipmentSell.equipmentStatus ? deviceStatusStr : '请选择设备状态'}}
					</view>
					<u-icon name="arrow-down"></u-icon>
				</view>
			</view>
			<view class="form-column-item">
				<view class="label">商品标题</view>
				<view class="content">
					<input type="text" v-model="form.productTitle" placeholder="请输入商品标题" />
				</view>
			</view>
			<view class="" v-if="productAttr.length">
				<view class="form-column-item" style="border:0;">
					<view class="label">商品参数(根据类别进行填写)</view>
				</view>
				<view class="form-column-item" v-for="(item,index) in productAttr" style="border-bottom: 1rpx solid #EBEBEB;">
					<view class="label" style="font-size: 28rpx;">{{item.title}}</view>
					<view class="content">
						<input v-model="item.value" type="text" :placeholder="'请输入' + item.title" />
					</view>
				</view>
			</view>
			<view class="form-column-item">
				<view class="label">商品图片</view>
				<view class="content">
					<UploadImages :limit="6" v-model="productImgList"></UploadImages>
				</view>
			</view>
			<view class="form-column-item">
				<view class="label">商品详情</view>
				<view class="content">
					<UploadImages :limit="6" v-model="productDetailImgList"></UploadImages>
				</view>
			</view>
			<view class="form-column-item">
				<view class="label">联系人</view>
				<view class="content">
					<input type="text" v-model="form.addressName" placeholder="请输入联系人" />
				</view>
			</view>
			<view class="form-column-item">
				<view class="label">联系方式</view>
				<view class="content">
					<input type="tel" v-model="form.addressPhone" maxlength="11" placeholder="请输入联系方式" />
				</view>
			</view>
			<view class="form-column-item">
				<view class="label">位置信息</view>
				<view class="picker-content" @tap="$refs.region.open()">
					<view :class="{'placeholder-picker': !regionStr, 'picker-text': regionStr}">
						{{regionStr ? regionStr : '请选择位置信息'}}
					</view>
					<u-icon name="arrow-down"></u-icon>
				</view>
			</view>
		</view>
		
		<view class="info-area" v-if="form.productType === productType.device.value && form.putType === sellType.used.value && isUsedPrice">
			<view class="info-item">
				<view class="label">估车价</view>
				<view class="content">
					<text class="price">￥{{valuationMoney }}</text>
				</view>
			</view>
		</view>
		
		<!-- 新建二手设备 -->
		<view class="bottom-btn-wrapper" v-if="form.productType === productType.device.value && form.putType === sellType.used.value && !productId">
			<SubmitBtn text="一键估价" color="#FEA413" :customStyle="{width: '332rpx'}" v-if="!isUsedPrice" @tap="oneValuation"></SubmitBtn>
			<SubmitBtn text="一键出售" color="#FEA413" :customStyle="{width: '332rpx'}" v-else @tap="smsCodePopupOpen(1)"></SubmitBtn>
			<SubmitBtn text="立即发布" :customStyle="{width: '332rpx'}" v-if="!isUsedPrice" @tap="submit"></SubmitBtn>
			<SubmitBtn text="平台验机" :customStyle="{width: '332rpx'}" v-else @tap="checkDevicePopupOpen"></SubmitBtn>
		</view>
		
		
		<view class="bottom-btn-wrapper" v-else>
			<SubmitBtn :text="!productId ? '立即发布' : '编辑'" @tap="submit"></SubmitBtn>
		</view>


		<OnePicker ref="productType" keyName="label" :columns="[productTypeArr]" @pickerConfirm="productTypeConfirm" :defaultIndex="[productTypeIndex]"></OnePicker>
		<OnePicker ref="sellType" keyName="label" :columns="[sellTypeArr]" @pickerConfirm="sellTypeConfirm" :defaultIndex="[sellTypeIndex]"></OnePicker>
		<OnePicker ref="payType" keyName="label" :columns="[payTypeArr]" @pickerConfirm="payTypeConfirm" :defaultIndex="[payTypeIndex]"></OnePicker>
		<OnePicker ref="deviceStatus" keyName="label" :columns="[deviceStatusArr]" @pickerConfirm="deviceStatusConfirm" :defaultIndex="[deviceStatusIndex]"></OnePicker>
		<u-datetime-picker :show="showFactoryDate" v-model="factoryDate"  mode="date" confirmColor="#FE5A13" @confirm="factoryDateConfirm" @cancel="showFactoryDate = false"></u-datetime-picker>
		<OnePicker ref="oldNewStatus" keyName="label" :columns="[oldNewStatusArr]" @pickerConfirm="oldNewStatusConfirm" ></OnePicker>
		<RegionPicker ref="region" @regionConfirm="regionConfirm" :provinceId="form.provinceId" :cityId="form.cityId" :areaId="form.areaId"></RegionPicker>
		
		<u-popup :show="showSmsCodePopup" mode="center" :close-on-click-overlay="true" :round="15" :safeAreaInsetBottom="false" @close="smsCodePopupClose">
			<view class="popup-content">
				<view class="title flex-xy-center">
					{{showSmsCodeType === 1 ? '一键出售' : '平台验机'}}
				</view>
				<view class="phone-form">
					<view class="form-item">
						<view class="label">手机号</view>
						<view class="content">
							<input type="text" placeholder="请输入手机号" v-model="form.phone"/>
						</view>
					</view>
					<view class="form-item">
						<view class="label">验证码</view>
						<view class="content">
							<input type="number" placeholder="请输入验证码" v-model="form.code" />
							<view class="sms-code flex-xy-center" @tap="sendSms" :class="{disabled: timer}">
								{{timer ? countdown + 's后重发' : '获取验证码'}}
							</view>
						</view>
					</view>
				</view>
				<SubmitBtn text="提交" color="#06BB20" :customStyle="{'font-size': '28rpx', width: '460rpx', height: '80rpx'}" @tap="submit"></SubmitBtn>
			</view>
		</u-popup>
		
		<u-popup :show="showCheckDevicePopup" mode="center" :close-on-click-overlay="true" :round="15" :safeAreaInsetBottom="false" @close="checkDevicePopupClose">
			<view class="popup-content">
				<view class="title flex-xy-center">验机说明</view>
				<view class="content-text">
					Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean euismod bibendum laoreet. Proin gravida dolor sit amet lacus accumsan et viverra justo commodo. Proin sodales pulvinar sic tempor. Sociis natoque penatibus et magnis dis parturientLorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean euismod bibendum laoreet. Proin gravida dolor sit amet lacus accumsan et viverra justo commodo. Proin sodales pulvinar sic tempor. Sociis natoque penatibus et magnis dis parturient
				</view>
				<view class="title flex-xy-center">验机收费</view>
				<view class="content-text">
					Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean euismod bibendum laoreet. Proin gravida dolor sit amet lacus accumsan et viverra justo commodo. Proin sodales pulvinar sic tempor. Sociis natoque penatibus et magnis dis parturientLorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean euismod bibendum laoreet. Proin gravida dolor sit amet lacus accumsan et viverra justo commodo. Proin sodales pulvinar sic tempor. Sociis natoque penatibus et magnis dis parturient
				</view>
				<SubmitBtn text="同意" color="#06BB20" :customStyle="{'font-size': '28rpx', width: '460rpx', height: '80rpx'}" @tap="agreeCheckDevice"></SubmitBtn>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		sellType,
		productType,
		payType,
		deviceStatus,
		oldNewStatus,
		const2Arr
	} from '@/consts/index.js'
	import { toast } from '@/utils/uni-api.js'
	import validate from '@/utils/validate.js'
	import {
		sendSmsCode
	} from '@/api/common.js'
	import { getCategory, cateProductParam, addProduct, getProductDetail, oneValuation, editProduct } from '@/api/product.js'
	export default {
		data() {
			return {
				sellType,
				productType,
				payType,
				deviceStatus,
				oldNewStatus,
				sellTypeArr: const2Arr(sellType),
				productTypeArr: const2Arr(productType),
				payTypeArr: const2Arr(payType),
				deviceStatusArr: const2Arr(deviceStatus),
				oldNewStatusArr: const2Arr(oldNewStatus),
				categoryList:[],
				productId: '',
				factoryDate: '',
				form: {
					productType: productType.device.value, // 1设备 2建材
					putType: sellType.factory.value,  // 1厂家 2二手 3租赁（租赁前端已拆分）
					categoryId: '',   // 分类ID
					productTitle: '', // 商品标题
					productImg: '',   // 商品图片
					intro: '', // 商品详情图片
					addressName: '',  // 联系人
					addressPhone: '', //联系方式
					provinceId: '',   // 省级ID
					province: '',
					cityId: '',       // 市级ID
					city: '',
					areaId: '',       // 地区ID
					area: '',
					productAttr: [],  // 商品参数
					showPrice: '',   // 展示价格 等同于售价
					address: '',  // 地址详情 暂时没用
					phone: '',
					code: '',
					// 出售设备
					hgProductEquipmentSell:{  
						equipmentBrand: '', //设备品牌
						equipmentNo: '', // 设备型号
						serialNo: '',  // 整机序列号
						factoryDate: '', //出厂日期
						workHours: '',  // 工作小时
						price: '', // 售价
						paymentWay: '',  //付款方式
						equipmentStatus: '', // 设备状态
						buyPrice: '', // 购机价格
						status: ''    // 机况状态
					}, 
					// 出售建材
					hgProductMaterialSell:{  
						stock: '',
						price: '',
						paymentWay: ''
					},
					// 租赁设备
					hgProductEquipmentLease:{
						equipmentBrand: '',
						equipmentNo: '',
						serialNo: '',
						factoryDate: '',
						workHours: '',
						monthRent: '',
						classRent: '',
						hourRent: '',
						leaseStatus: '' // 租赁状态 1-待租 2-已租
					},
					// 租赁建材
					hgProductMaterialLease:{
						dayRent: '',
						monthRent: '',
						yearRent: '',
						stock: '',
						leaseStatus: '' // 租赁状态 1-待租 2-已租
					},
					// 规格
					hgProductSkus: [], 	 
				},
				productImgList: [], // 商品图片数组
				productDetailImgList: [], // 商品详情图片数据
				showFactoryDate: false, // 显示出厂日期Picker
				showSmsCodePopup: false,
				showCheckDevicePopup: false,
				showSmsCodeType: 1  , // 1 一键出售  2：平台验机
				isUsedPrice: false, // 二手设备 是否已估价
				countdown: 59,
				timer: '',
				productAttr: [],
				productTypeIndex: 0,
				sellTypeIndex: 0,
				payTypeIndex: 0, 
				deviceStatusIndex: 0,
				valuationMoney: 0
			}
		},
		computed: {
			productTypeStr() {
				return this.productTypeArr.find(item => item.value === this.form.productType).label
			},
			sellTypeStr() {
				return this.sellTypeArr.find(item => item.value === this.form.putType).label
			},
			payTypeStr(){
				return this.form.hgProductEquipmentSell.paymentWay ? this.payTypeArr.find(item => item.value === this.form.hgProductEquipmentSell.paymentWay).label : ''
			},
			deviceStatusStr(){
				return this.form.hgProductEquipmentSell.equipmentStatus ? this.deviceStatusArr.find(item => item.value === this.form.hgProductEquipmentSell.equipmentStatus).label : ''
			},
			oldNewStatusStr(){
				return this.form.hgProductEquipmentSell.status ? this.oldNewStatusArr.find(item => item.value === this.form.hgProductEquipmentSell.status).label : ''
			},
			regionStr(){
				return this.form.provinceId ? `${this.form.province},${this.form.city},${this.form.area}` : ''
			}
		},
		onLoad(option){
			if(option.id){
				this.productId = option.id
				this.getProductDetail(option.id)
			}
			this.getCategory()
		},
		methods: {
			getCategory(){
				getCategory().then(res => {
					this.categoryList = this.formatCategory(res.result);
				})
			},
			getProductDetail(id){
				getProductDetail({id}).then(res => {
					let data = res.result;
					this.form = {
						productType: data.product_type, 
						putType: data.put_type,  
						categoryId: data.category_id, 
						productTitle: data.product_title, // 商品标题
						productImg: '',   // 商品图片
						intro: '',        // 商品详情图片
						addressName: data.address_name,  // 联系人
						addressPhone: data.address_phone, //联系方式
						provinceId: data.province_id,   // 省级ID
						province: data.province,
						cityId: data.city_id,       // 市级ID
						city: data.city,
						areaId: data.area_id,       // 地区ID
						area: data.area,
						productAttr: [],  // 商品参数
						showPrice: '',   // 展示价格 等同于售价
						address: '',  // 地址详情 暂时没用
						phone: '',
						code: '',
						// 出售设备
						hgProductEquipmentSell:{  
							equipmentBrand: data.hgProductEquipmentSell ? data.hgProductEquipmentSell.equipmentBrand : '', //设备品牌
							equipmentNo: data.hgProductEquipmentSell ? data.hgProductEquipmentSell.equipmentNo : '', // 设备型号
							serialNo: data.hgProductEquipmentSell ? data.hgProductEquipmentSell.serialNo : '',  // 整机序列号
							factoryDate: data.hgProductEquipmentSell ? data.hgProductEquipmentSell.factoryDate : '', //出厂日期
							workHours: data.hgProductEquipmentSell ? data.hgProductEquipmentSell.workHours : '',  // 工作小时
							price: data.hgProductEquipmentSell ? data.hgProductEquipmentSell.price : '', // 售价
							paymentWay: data.hgProductEquipmentSell ? data.hgProductEquipmentSell.paymentWay : '',  //付款方式
							equipmentStatus: data.hgProductEquipmentSell ? data.hgProductEquipmentSell.equipmentStatus : '', // 设备状态
							buyPrice: data.hgProductEquipmentSell ? data.hgProductEquipmentSell.buyPrice : '', // 购机价格
							status: data.hgProductEquipmentSell ? data.hgProductEquipmentSell.status : ''    // 机况状态
						}, 
						// 出售建材
						hgProductMaterialSell:{  
							stock: data.hgProductMaterialSell ? data.hgProductMaterialSell.stock : '',
							price: data.hgProductMaterialSell ? data.hgProductMaterialSell.price : '',
							paymentWay: data.hgProductMaterialSell ? data.hgProductMaterialSell.paymentWay : '',
						},
						// 租赁设备
						hgProductEquipmentLease:{
							equipmentBrand: '',
							equipmentNo: '',
							serialNo: '',
							factoryDate: '',
							workHours: '',
							monthRent: '',
							classRent: '',
							hourRent: '',
							leaseStatus: '' // 租赁状态 1-待租 2-已租
						},
						// 租赁建材
						hgProductMaterialLease:{
							dayRent: '',
							monthRent: '',
							yearRent: '',
							stock: '',
							leaseStatus: '' // 租赁状态 1-待租 2-已租
						},
						hgProductSkus: data.hgProductSkuList ? data.hgProductSkuList.map(item => ({skuName: item.skuName, skuPrice: item.skuPrice, skuStock: item.skuStock})) : []
						
					}
					this.productImgList = data.product_img.split(',')
					this.productDetailImgList = data.intro.split(',')
					this.factoryDate = this.form.hgProductEquipmentSell ? new Date(this.form.hgProductEquipmentSell.factoryDate) : ''
					this.productTypeIndex = this.productTypeArr.findIndex(item => item.value == this.form.productType)
					this.sellTypeIndex = this.sellTypeArr.findIndex(item => item.value === this.form.putType)
					this.payTypeIndex = this.payTypeArr.findIndex(item => item.value === this.form.paymentWay)
					this.deviceStatusIndex = this.deviceStatusArr.findIndex(item => item.value === this.form.equipmentStatus)
					
					const attrs = JSON.parse(data.product_attr)
					cateProductParam({id: this.form.categoryId}).then(cates => {
						if(cates.result.categoryAttr){
							const allCategory = cates.result.categoryAttr.split(',')
							for (var i = 0; i < allCategory.length; i++) {
								const isExitsAttr = attrs.find(item => item.title === allCategory[i])
								this.productAttr.push({title: allCategory[i], value: isExitsAttr ? isExitsAttr.value : ''});
							}	
						}
					})
				})
			},
			formatCategory(data){
				data.forEach(item => {
					item.text = item.categoryTitle
					item.value = item.id
					if(item.children){
						this.formatCategory(item.children)
					}
				})
				return data;
			},
			productTypeConfirm(e) {
				this.form.productType = e.value.value
			},
			sellTypeConfirm(e) {
				this.form.putType = e.value.value
			},
			onchange(e) {
				const value = e.detail.value
				// if(!value.length) return 
				// if(value[value.length - 1].value.split('$')[1]  != 4){
				// 	this.$refs.category.clear()
				// 	return toast('请选择最后一级分类')
				// }
				if(!this.form.categoryId){
					this.productAttr = []
					return 
				}
				
				cateProductParam({id: this.form.categoryId}).then(res => {
					if(res.result && res.result.categoryAttr){
						this.productAttr = []
						res.result.categoryAttr.split(',').forEach(item => {
							this.productAttr.push({title: item, value: ''});
						})
					}
				})
			},
			onnodeclick(node) {},
			payTypeConfirm(e){
				if(this.form.productType === productType.device.value){
					this.form.hgProductEquipmentSell.paymentWay = e.value.value
				}else{
					this.form.hgProductMaterialSell.paymentWay = e.value.value
				}
				
			},
			deviceStatusConfirm(e){
				this.form.hgProductEquipmentSell.equipmentStatus = e.value.value
			},
			factoryDateConfirm(e){
				this.form.hgProductEquipmentSell.factoryDate = this.$moment(e.value).format('YYYY-MM-DD')
				this.showFactoryDate = false
			},
			oldNewStatusConfirm(e){
				this.form.hgProductEquipmentSell.status = e.value.value
			},
			regionConfirm(e){
				this.form.provinceId = e.value[0].value
				this.form.cityId = e.value[1].value
				this.form.areaId = e.value[2].value
				this.form.province = e.value[0].label
				this.form.city = e.value[1].label
				this.form.area = e.value[2].label
			},
			smsCodePopupOpen(type){
				this.timer = ''
				this.countdown = 59
				this.form.phone = ''
				this.form.code = ''
				if(!this.checkBeforeSubmit()) return
				this.showSmsCodePopup = true
				this.showSmsCodeType = type
			},
			smsCodePopupClose(){
				this.showSmsCodePopup = false
			},
			checkDevicePopupOpen(){
				if(!this.checkBeforeSubmit()) return
				this.showCheckDevicePopup = true
			},
			checkDevicePopupClose(){
				this.showCheckDevicePopup = false
			},
			agreeCheckDevice(){
				this.checkDevicePopupClose()
				this.smsCodePopupOpen(2)
			},
			sendSms() {
				if (!validate.isNotEmpty(this.form.phone, '手机号')) return
				if (!validate.isPhone(this.form.phone, '手机号')) return
				if (this.timer) return
				sendSmsCode({
					phone: this.form.phone,
					use: 2
				}).then(res => {
					toast('验证码发送成功')
					this.timer = setInterval(() => {
						this.countdown--;
						if (this.countdown < 1) {
							clearInterval(this.timer)
							this.timer = ''
							this.countdown = 59
						}
					}, 1000)
				})
			},
			oneValuation(){
				if(!validate.isNotEmpty(this.form.hgProductEquipmentSell.buyPrice, '购机价格')) return
				if(!validate.isNotEmpty(this.form.hgProductEquipmentSell.factoryDate, '出厂日期')) return
				if(!validate.isNotEmpty(this.form.hgProductEquipmentSell.workHours, '工作小时')) return
				if(!validate.isNotEmpty(this.form.hgProductEquipmentSell.status, '机况状态')) return
				oneValuation({
					buyPrice: this.form.hgProductEquipmentSell.buyPrice, 
					dateForMade: this.form.hgProductEquipmentSell.factoryDate,
					workHour: this.form.hgProductEquipmentSell.workHours,
					status: this.form.hgProductEquipmentSell.status
				}).then(res => {
					this.valuationMoney = res.result
					this.isUsedPrice = true
				})
			},
			checkBeforeSubmit(){
				if(!validate.isNotEmpty(this.form.categoryId, '分类')) return
				// 出售设备
				if(this.form.productType === this.productType.device.value){
					if(!validate.isNotEmpty(this.form.hgProductEquipmentSell.equipmentBrand, '设备品牌')) return
					if(!validate.isNotEmpty(this.form.hgProductEquipmentSell.equipmentNo, '设备型号')) return
					
					// 厂家设备
					if(this.form.putType === this.sellType.factory.value){
						if(!this.form.hgProductSkus.length){
							return toast('请设置规格')
						}
					}
					// 二手设备
					else {
						if(!validate.isNotEmpty(this.form.hgProductEquipmentSell.factoryDate, '出厂日期')) return
						if(!validate.isNotEmpty(this.form.hgProductEquipmentSell.workHours, '工作小时')) return
						if(!validate.isNotZero(this.form.hgProductEquipmentSell.workHours, '工作小时')) return
					}
					if(!validate.isNotEmpty(this.form.hgProductEquipmentSell.serialNo, '整机序列号')) return
					if(!validate.isNotEmpty(this.form.hgProductEquipmentSell.price, '售价')) return
					if(!validate.isNotZero(this.form.hgProductEquipmentSell.price, '售价')) return
					if(!validate.isNotEmpty(this.form.hgProductEquipmentSell.equipmentStatus, '设备状态')) return
				}
				// 出售建材
				else {
					if(!validate.isNotEmpty(this.form.hgProductMaterialSell.stock, '库存')) return
					if(!validate.isNotZero(this.form.hgProductMaterialSell.stock, '库存')) return
					if(!validate.isNotEmpty(this.form.hgProductMaterialSell.price, '单价')) return
					if(!validate.isNotZero(this.form.hgProductMaterialSell.price, '单价')) return
					
				}
				
				if(this.form.putType === sellType.factory.value){
					if(!validate.isNotEmpty(this.form.hgProductEquipmentSell.paymentWay, '付款方式')) return
				}
				if(!validate.isNotEmpty(this.form.productTitle, '商品标题')) return
				
				for (var i = 0; i < this.productAttr.length; i++) {
					if(!this.productAttr[i].value){
						return toast(this.productAttr[i].title + '不能为空')
					}
				}
				if(!this.productImgList.length){
					return toast('请上传商品图片');
				}
				if(!this.productDetailImgList.length){
					return toast('请上传商品详情图片');
				}
				if(!validate.isNotEmpty(this.form.addressName, '联系人')) return
				if(!validate.isNotEmpty(this.form.addressPhone, '联系方式')) return
				if(!validate.isNotEmpty(this.form.provinceId, '位置信息')) return
				
				return true
			},
			submit(){
				if(!this.checkBeforeSubmit()) return
				
				this.form.productImg = this.productImgList.join(',')
				this.form.intro = this.productDetailImgList.join(',')
				this.form.productAttr = JSON.stringify(this.productAttr)
				this.form.showPrice = this.form.hgProductEquipmentSell.price
				// 厂家设备
				if(this.form.productType === productType.device.value && this.form.putType === sellType.factory.value){
					this.form.isSku = 1
				}else{
					this.form.isSku = 2
				}
				
				
				if(!this.productId){
					addProduct(this.form).then(res => {
						toast(res.message, true)
						setTimeout(() => {
							uni.navigateBack()
						}, 800)
					})
				}else{
					this.form.id = this.productId
					editProduct(this.form).then(res => {
						uni.setStorageSync('refresh', 1)
						toast(res.message, true)
						setTimeout(() => {
							uni.navigateBack()
						}, 800)
					})
				}
				
			}
			
		}
	}
</script>

<style lang="scss">
	::v-deep .input-value-border {
		border: 0 !important;
	}
	
	::v-deep .input-value{
		height: auto !important;
		padding: 0 !important;
		padding-top: 20rpx !important;
		font-size: 31rpx !important;
		color: #333 !important;
	}
	
	::v-deep .uni-data-tree{
		flex:1;
	}
	
	.add-sku{
		width: 231rpx;
		height: 67rpx;
		background: #FFF5F0;
		border-radius: 6rpx;
		border: 1rpx solid $theme-color;
		font-weight: normal;
		font-size: 29rpx;
		color: $theme-color;
	}
	
	.store-unit{
		font-size: 31rpx;
		color: #333333;
		margin-left: 20rpx;
	}
	
	.popup-content{
		width: 577rpx;
		background: #FFFFFF;
		border-radius: 15rpx 15rpx 15rpx 15rpx;
		padding: 50rpx 35rpx 40rpx;
		box-sizing: border-box;
		
		.title{
			font-size: 33rpx;
			color: #333333;
			font-weight: bold;
		}
		
		.content-text{
			height: 280rpx;
			overflow-y: auto;
			font-weight: normal;
			font-size: 27rpx;
			color: #333333;
			line-height: 38rpx;
			margin: 30rpx 0;
		}
		
		.phone-form{
			padding: 25rpx 0 40rpx;
			
			.form-item{
				display: flex;
				align-items: center;
				border-bottom: 1rpx solid #EBEBEB;
				padding: 20rpx 0;
				
				.label{
					padding-right: 40rpx;
					flex-shrink: 0;
				}
				
				.content{
					display: flex;
					align-items: center;
					
					input{
						flex: 1;
						font-size: 29rpx;
					}
				}
			}
		}
	}
	
	.sms-code {
		width: 163rpx;
		height: 52rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
		border: 1rpx solid $theme-color;
		font-weight: normal;
		font-size: 25rpx;
		color: $theme-color;
		margin-left: 15rpx;
	}
	
	.disabled {
		border: 1rpx solid #d1d1d1;
		color: #d1d1d1;
	}
</style>